package com.waypolice.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.waypolice.manager.controller.BorrowAuditAction;
import com.waypolice.manager.service.BorAuditService;
import com.waypolice.mapper.CmsBorAuditingMapper;
import com.waypolice.mapper.handwriting.CmsBorAuditingMapperSon;
import com.waypolice.mapper.handwriting.CmsBorRegisterExtMapper;
import com.waypolice.pojo.CmsBorAuditing;
import com.waypolice.pojo.vo.CmsBorRegisterVo;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author zyp
 * @description 借阅审核service实现类
 * @create 2019-03-12 11:43
 */
@Service
public class BorAuditServiceImpl implements BorAuditService {

    private static final Logger logger = LoggerFactory.getLogger(BorrowAuditAction.class);

    @Autowired
    private CmsBorRegisterExtMapper cmsBorRegisterExtMapper;

    @Autowired
    private CmsBorAuditingMapper cmsBorAuditingMapper;

    @Autowired
    private CmsBorAuditingMapperSon cmsBorAuditingMapperSon;

    /**
     * @author:zyp
     * @description 根据登记id，获取登记记录
     * @date: 2019/3/12 11:44
     * @return
     */
    @Override
    public CmsBorRegisterVo getBorRegisterVoById(long id) {
        CmsBorRegisterVo cmsBorRegisterVo = cmsBorRegisterExtMapper.getBorRegisterVoById(id);
        return cmsBorRegisterVo;
    }

    /**
     * @author:zyp
     * @description 插入审核数据
     * @date: 2019/3/12 14:26
     * @return 
     */
    @Override
    public void insert(CmsBorAuditing cmsBorAuditing) {
        cmsBorAuditingMapper.insertSelective(cmsBorAuditing);
    }

    /**
     * @author:zyp
     * @description 根据登录id获取（卷）件的信息
     * @date: 2019/3/20 10:53
     * @return
     */
    @Override
    public CmsBorRegisterVo getFileInfoByRegistId(CmsBorRegisterVo cmsBorRegisterVo) {
        List<Map<String, String>> fileInfoLists = cmsBorRegisterExtMapper.getFileInfoByRegistId(cmsBorRegisterVo.getId());
        StringBuilder sb = new StringBuilder();
        if(fileInfoLists!=null&&fileInfoLists.size()!=0){
            if (fileInfoLists.size()==1){
                cmsBorRegisterVo.setBorFileName(fileInfoLists.get(0).get("BORFILENAME"));
                cmsBorRegisterVo.setVolCaseNum(fileInfoLists.get(0).get("EXTEND1"));
            }else{
                for(Map<String, String> fileInfo : fileInfoLists){
                    sb.append(String.valueOf(fileInfo.get("BORFILENAME"))+";");
                }
                cmsBorRegisterVo.setBorFileName(sb.toString().substring(0,sb.toString().lastIndexOf(";")));
                cmsBorRegisterVo.setVolCaseNum("");
            }
        }else{
            cmsBorRegisterVo.setBorFileName("");
            cmsBorRegisterVo.setVolCaseNum(" ");
        }
        return cmsBorRegisterVo;
    }

    /**
     * @author:zyp
     * @description 根据借阅登记id，获取当前登记的id的最后一次的审核信息
     * @date: 2019/3/20 17:03
     * @return 
     */
    @Override
    public CmsBorAuditing getModifyAppliInfo(Long borId) {
        CmsBorAuditing cmsBorAuditing = cmsBorAuditingMapperSon.getModifyAppliInfo(borId);
        return cmsBorAuditing;
    }

    /**
     * @author:zyp
     * @description 查找我的历史审批记录 包含所以我审批的所有流程显示
     * @date: 2019/3/22 15:17
     * @return 
     */
    @Override
    public CmsBorRegisterVo getMyAuditRegister(Long borId, Long userId) {
        Map<String,Object> parameterMap = new HashMap<>(2);
        parameterMap.put("borId",borId);
        parameterMap.put("userId",userId);
        CmsBorRegisterVo cmsBorRegisterVo = cmsBorRegisterExtMapper.getMyAuditRegister(parameterMap);
        return cmsBorRegisterVo;
    }

    /**
     * @author:zyp
     * @description  根据登录id 以及针对性获取各流程的数据（借阅流程、检索流程）
     * @date: 2019/3/25 10:12
     * @return 
     */
    @Override
    public CmsBorRegisterVo getBorRegisterVoByIdAndActType(Long borId, String activitiType) {
        Map<String,Object> parameterMap = new HashMap<>();
        parameterMap.put("id",borId);
        switch (activitiType){
            case "searchAct":
                parameterMap.put("taskType",2);
                break;
            case "renewBor,borAct":
                parameterMap.put("taskType",1);
                break;
                default:
                    parameterMap.put("taskType",0);
        }
        CmsBorRegisterVo cmsBorRegisterVo = cmsBorRegisterExtMapper.getBorRegisterVoByIdAndActType(parameterMap);
        return cmsBorRegisterVo;
    }

    /**
     * @author:zyp
     * @description 根据借阅id，获取借阅的文件信息
     * @date: 2019/4/18 16:23
     * @return 
     */
    @Override
    public Map<String, Object> getBorrowInfoByBorId(Integer offset, Integer limit, String sort, String order,Long borId) {
        Map<String, Object> result=new HashedMap();
        PageHelper.offsetPage(offset, limit, false);
        List<Map<String, String>> maps = cmsBorRegisterExtMapper.getBorrowInfoByBorId(borId);
        int count = cmsBorRegisterExtMapper.getCountBorrowInfoByBorId(borId);
        result.put("rows", maps);
        result.put("total", 1);
        return result;
    }
}